package com.nd.mobile.study.common.util;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fuckhtc.gson.JsonDeserializationContext;
import com.fuckhtc.gson.JsonDeserializer;
import com.fuckhtc.gson.JsonElement;
import com.fuckhtc.gson.JsonParseException;

public class DateDeserializer implements JsonDeserializer<Date>{

    private static final String JSONDateToMilliseconds = "\\/(Date\\((-*.*?)([\\+\\-].*)?\\))\\/";
    private static final Pattern pattern = Pattern.compile(JSONDateToMilliseconds);
    
    @Override
    public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
    	Matcher matcher = pattern.matcher(json.getAsJsonPrimitive().getAsString());
        String result = matcher.replaceAll("$2");
		long milliseconds = Long.parseLong(result);
        
        return new Date(milliseconds);
    }
}